<template>
  <div>
    <el-dialog  :visible.sync="previewDialog" :width="dialogWidth">
      <img  :src="dataForm.picUrl" alt="" @load="onLoad">
    </el-dialog>

    <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="轮播图" prop="picUrl">
      <div v-if="changeFlag">
      <single-upload
        :img="dataForm.picUrl"
        @uploadSuccess="updateDetails"
      ></single-upload></div>
      <div v-else>

        <img :src="dataForm.picUrl" alt="" class="avatar" >
      </div>
      <el-button type="primary" @click="handlePreview">预览</el-button>
    </el-form-item>
<!--    <el-form-item label="逻辑删除位【0删除，1存在】" prop="showStatus">-->
<!--      <el-input v-model="dataForm.showStatus" placeholder="逻辑删除位【0删除，1存在】"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="创建时间" prop="createTime">-->
<!--      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>-->
<!--    </el-form-item>-->

      <el-form-item label="是否展示" prop="isShow">
        <el-radio-group v-model="dataForm.isShow">
          <el-radio :label="0">不展示</el-radio>
          <el-radio :label="1">展示</el-radio>
        </el-radio-group>
      </el-form-item>
    <el-form-item label="排序字段" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
  import SingleUpload from '../../../components/upload/singleUpload'
  import Detail from '../../../components/detail'
  export default {
    components: {Detail, SingleUpload},

    data () {
      const setSort = (rule, value, callback) => {

        var regPos = /^(([1-9]\d*)(\.\d+)?)$|^0\.\d*[1-9]$|^0$/; //非负浮点数
        if(value <= 0){
          return callback(new Error("排序字段要大于0"))
        }else if(!regPos.test(value)){
          return callback(new Error("排序字段不能为非数字类型"))
        }
        else{
          return callback()
        }
      };
      return {
        dialogWidth:0,
        changeFlag:false,
        previewDialog:false,
        visible: false,
        dataForm: {
          id: 0,
          picUrl: '',
          showStatus: '',
          createTime: '',
          isShow: '',
          sort: ''
        },
        dataRule: {
          picUrl: [
            { required: true, message: '轮播图的url不能为空', trigger: 'blur' }
          ],
          showStatus: [
            { required: true, message: '逻辑删除位【0删除，1存在】不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          isShow: [
            { required: true, message: '是否在前台展示【0不显示，1显示】不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段不能为空', trigger: 'blur' },
            {validator:setSort,trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      onLoad(e){
        const img = e.target;
        let width = 0;
        if (img.fileSize > 0 || (img.width > 1 && img.height > 1)) {
          width = img.width + 40;
        }
        this.dialogWidth = width + 'px';
      },
      updateDetails(imgUrl){
        this.dataForm.picUrl=imgUrl;
      },
      handlePreview () {
        this.previewDialog=true;
      },
      init (id) {
        this.changeFlag=false;
        console.log(id)
        this.dataForm.id = id || 0
        if(id===null||id===undefined)this.changeFlag=true;
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/propaganda/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.picUrl = data.propaganda.picUrl
                this.dataForm.showStatus = data.propaganda.showStatus
                this.dataForm.createTime = data.propaganda.createTime
                this.dataForm.isShow = data.propaganda.isShow
                this.dataForm.sort = data.propaganda.sort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/propaganda/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'picUrl': this.dataForm.picUrl,
                'showStatus': this.dataForm.showStatus,
                'createTime': this.dataForm.createTime,
                'isShow': this.dataForm.isShow,
                'sort': this.dataForm.sort
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-bottom: 2%;
}
</style>
